package com.ruipengkj.view.workShiftView;
/**
 * 说明：交班记录
 * 
 * @Author：孙航
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TWorkShift;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.page.PageDelegale;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.WorkShiftModel;
import com.ruipengkj.service.TWorkShiftService;
import com.ruipengkj.view.base.BasePagePanel;

public class WorkShiftTakeNotesPanel extends BasePagePanel<TWorkShift> implements PageDelegale ,Runnable{

	private static final long serialVersionUID = 1L;
	
	private JButton queryButton; //查询
	
	private JTable table;
	private JScrollPane tableJsp;
	private WorkShiftModel workShiftModel;
	private DataPackageComponent startTimeTF = null;//发卡时间
	private DataPackageComponent timeTF = null;//时间
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TWorkShift> pageView;//翻页主键
	private TWorkShiftService tWorkShiftService=BuildSpringApplication.getInstance().getTWorkShiftServiceImpl();;
	
	public WorkShiftTakeNotesPanel() {
		setLayout(new BorderLayout());
		init();
		initListener();
	}
	
	private JPanel initTop() {
		JPanel top_panel = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		startTimeTF = new DataPackageComponent("交班时间：", 15);
		timeTF = new DataPackageComponent("时间：", 15);
		
		queryButton = new JButton("查询");
		
		
		top_panel.add(startTimeTF);
		top_panel.add(timeTF);
		top_panel.add(queryButton);
		return top_panel;
	}
	
	private void init() {
		pageView = new PageView<TWorkShift>();
		workShiftModel = new WorkShiftModel();
		table=uiFactory.createRPJTable(workShiftModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.add(initTop(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(pagingComponent,BorderLayout.SOUTH);
		//排序
		TableRowSorter<WorkShiftModel> tableRowSorter = new TableRowSorter<WorkShiftModel>(workShiftModel);  
		table.setRowSorter(tableRowSorter);
		this.termQuery(1);
	}
	
	//初始化监听器
	private void initListener() {
		queryButton.addActionListener(this);
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TWorkShift tWorkShift=new TWorkShift();
		tWorkShift.setToEmployeeID(ApplicationContext.EMPLOYEE.getEmployeeID());
		tWorkShift.setHappenTime(startTimeTF.getDate());
		tWorkShift.setEndTime(timeTF.getDate());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TWorkShift> pageModel = tWorkShiftService.getScrollByEmployee(pageView.getFirstResult(), pageView.getMaxResult(),tWorkShift);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						Util.clearTableRowSorter(table);
						workShiftModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TWorkShift> getPageView() {
		return this.pageView;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询按钮
			this.termQuery(1);
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
